package person.bing.fbcloud.auth.config;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import person.bing.fbcloud.SysUserInfoVo;
import person.bing.fbcloud.common.core.exception.CheckedException;
import person.bing.fbcloud.common.core.util.EncryptSecondUtil;
import person.bing.fbcloud.common.core.util.R;
import person.bing.fbcloud.entity.SysUser;
import person.bing.fbcloud.service.UpmsService;

import java.util.Arrays;
import java.util.List;

/**
 * @author: mengfanbing
 * @date: 2020/1/6 14:33
 */
@Service
public class FbcloudUserService implements UserDetailsService {
    @Autowired
    private UpmsService upmsService;
    @Autowired
    private PasswordEncoder encoder;
    @Override
    @SneakyThrows
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        EncryptSecondUtil encryptSecondUtil = new EncryptSecondUtil();

        JSONObject params = new JSONObject();
        params.put("userName",username);
        R<SysUserInfoVo> userInfoByNameR = upmsService.getUserInfoByName(params);
        int code = userInfoByNameR.getCode();
        SysUserInfoVo sysUserInfoVo = userInfoByNameR.getData();
        if (code != 0){
            throw new CheckedException("系统错误，请联系管理员");
        }
        if (ObjectUtil.isNull(sysUserInfoVo) || ObjectUtil.isNull(sysUserInfoVo.getSysUser())){
            throw new UsernameNotFoundException("系统用户：" + username + "不存在。");
        }
        List<String> permissionList = sysUserInfoVo.getPermissions();
        SysUser sysUser = sysUserInfoVo.getSysUser();
        String userCode = sysUser.getUserName();
        String userPwd = "";
        String userPwdGet = sysUser.getPassword();
        if (StrUtil.isNotEmpty(userPwdGet)){
            userPwd = userPwdGet;
        }
        String passwordDecode = encryptSecondUtil.AESdecode(userPwd, EncryptSecondUtil.AES);
        System.out.printf("密码解密，passwordDecode=%s \n",passwordDecode);
        //AuthorityUtils.commaSeparatedStringToAuthorityList("Admin") 用户角色？
        String[] permissions = new String[0];
        if (CollectionUtil.isNotEmpty(permissionList)){
            int size = permissionList.size();
            String[] permissionArray = new String[size];
            permissions = permissionList.toArray(permissionArray);
        }

        User user = new User(userCode,encoder.encode(passwordDecode), AuthorityUtils.createAuthorityList(permissions));
        return user;
    }
}
